package com.qqtop10.manager.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.Admin;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.AdminService;
import com.qqtop10.manager.utils.LogHelper;

@Controller
@RequestMapping("/")
public class LoginController extends BaseController {

	@Autowired
	private AdminService adminService;
	/*
	 * @Autowired private ApplicationContext ioc = new
	 * ClassPathXmlApplicationContext("ApplicationContext.xml");
	 */
	@Autowired
	private JdbcTemplate template;
//
//	@Autowired
//	@Qualifier("cookieManager")
//	private CookieManager cookieManager;

	@RequestMapping("login.do")
	public String login(Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {

		// RowMapper<Employee> rowMapper = new
		// BeanPropertyRowMapper<dat>(ha.class);
		List<Map<String, Object>> userAccountMap = template.queryForList("select * from tb_admin"); // ("select
																									// *
																									// from
																									// tb_admin");

		String account = getRequest().getParameter("account");
		String password = getRequest().getParameter("password");
		String remember = getRequest().getParameter("remember");
		boolean flag = account == null && password == null;
		if ((account == null || account.length() == 0) && getRequest().getCookies() != null) {
			for (Cookie item : getRequest().getCookies()) {
				if ("account".equals(item.getName())) {
					account = item.getValue();
					break;
				}
			}
		} else {
			Cookie cookie = new Cookie("account", account);
			cookie.setMaxAge(Integer.MAX_VALUE);
			getResponse().addCookie(cookie);
		}
		map.put("act", account);
		if ((password == null || password.length() == 0) && getRequest().getCookies() != null) {
			for (Cookie item : getRequest().getCookies()) {
				if ("password".equals(item.getName())) {
					password = item.getValue();
					break;
				}
			}
		}
		if (flag) {
			map.put("pwd", password);
			return "login";
		}
		if (account == null || password == null || account.length() == 0 || password.length() == 0) {
			map.put("msg", "请输入账号和密码。");
			return "login";
		}
		Admin admin = new Admin();
		admin.setAccount(account);
		admin = adminService.get(admin);
		if (admin == null || !password.equals(admin.getPassword())) {
			map.put("msg", "账号或密码错误！");
			return "login";
		}

		getRequest().getSession().setAttribute("Logind", admin);
		if ("1".equals(remember)) {
			Cookie cookie = new Cookie("password", password);
			cookie.setMaxAge(60 * 60 * 24 * 7);
			getResponse().addCookie(cookie);
		} else {
			Cookie cookie = new Cookie("password", "");
			cookie.setMaxAge(0);
			getResponse().addCookie(cookie);
		}
		Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");

		LogHelper.Log(Tempadmin.getAccount(), "管理后台模块", "登录成功", LogHelper.getLocalIp(getRequest()),
				Tempadmin.getAccount(), "", true);

		return "redirect:/manager/index.do";
	}

	@RequestMapping("modify-password.do")
	@ResponseBody
	public Result modifyPassword() {
		try {
			String oldPassword = getRequest().getParameter("oldPassword");
			String newPassword = getRequest().getParameter("newPassword");

			if (oldPassword == null || newPassword == null) {
				return err("有些必须的参数没有指定。");
			}
			oldPassword = md5(oldPassword).toLowerCase();
			newPassword = md5(newPassword).toLowerCase();
			Admin logind = getLogind();
			if (!oldPassword.equals(logind.getPassword().toLowerCase())) {
				return err("密码错误！");
			}
			logind.setMender(getLogindUuid());
			logind.setUpdatetime(now());
			logind.setPassword(newPassword);
			if (adminService.modifyPassword(logind)) {
				Admin Tempadmin = (Admin) getRequest().getSession().getAttribute("Logind");
				LogHelper.Log(Tempadmin.getAccount(), "管理后台模块", "密码修改成功", LogHelper.getLocalIp(getRequest()),
						Tempadmin.getAccount(), "", true);

				return ok("操作成功！");
			}
			logind.setPassword(oldPassword);

			return err("操作失败！");
		} catch (Exception e) {
			return excep(e);
		}
	}
}
